/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.model.CurrencyModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class CurrenciesComponent extends JSONServerComponent
{	
	protected CurrencyModel currencyModel;
	protected List<CurrencyModel> currencyModels;

	protected Grid bodyPanel;
	protected ListBox listBox;
	protected String defaultCode = "";
	protected String currentCode = "";
	protected String historyToken = "";

	/**
	 * Constructor
	 */
	public CurrenciesComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);

		currencyModel = new CurrencyModel(1, "US Dollar", "USD", 1.0000, true);
		currencyModels = new ArrayList<CurrencyModel>();

		bodyPanel = new Grid(1,1);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");

		listBox = new ListBox();
		listBox.addChangeHandler(changeHandler);
		bodyPanel.setWidget(0, 0, listBox);

		CellFormatter cf = bodyPanel.getCellFormatter();
		cf.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		if (params.length > 1) currentCode = params[1];

		int sz = currencyModels.size();
		for (int i = 0; i < sz; i++)
		{
			CurrencyModel model = currencyModels.get(i);
			if (model.getCode().equals(currentCode))
			{
				currencyModel.update(model);
				listBox.setSelectedIndex(i);
			}
		}
		if (historyToken == null || historyToken.length() == 0) historyToken = "CategoriesSummaryComponent--0";
		onSelection(historyToken);
		return false;
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		defaultCode = AbstractModel.convertToString(jsonObject, "default_code");
		currentCode = AbstractModel.convertToString(jsonObject, "current_code");
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		currencyModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		CurrencyModel model = new CurrencyModel(jsonObject, defaultCode);
		currencyModels.add(model);
		if (model.getCode().equals(currentCode))
		{
			currencyModel.update(model);
		}
	}

	public void buildFromModel() throws Exception
	{
		int sz = currencyModels.size();
		listBox.clear();
		for (int i = 0; i < sz; i++)
		{
			CurrencyModel model = currencyModels.get(i);
			String name = model.getName();
			String code = model.getCode();
			if (!code.equals(defaultCode))
			{
				name += " (approx. " + DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(model.getDateUpdated()) +")";
			}
			listBox.addItem(name, String.valueOf(model.getId()));
			if (model.getId() == currencyModel.getId())
			{
				listBox.setSelectedIndex(i);
			}
		}

		panel.setWidget(1, 0, bodyPanel);

		onBuilt();
	}

	protected ChangeHandler changeHandler = new ChangeHandler() {
		public void onChange(ChangeEvent event) {
			CurrencyModel model = getCurrencyModel(listBox.getSelectedIndex());
			historyToken = controller.getHistoryToken();
			onSelection("CurrenciesComponent--" + model.getCode());
		}
	};

	public CurrencyModel getCurrencyModel(int index) {
		return currencyModels.get(index);
	}

	public CurrencyModel getCurrencyModel() {
		return currencyModel;
	}
}
